﻿using HalconDotNet;
using MachineVision.Core;
using MachineVision.Shared.Controls;
using Microsoft.Win32;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MachineVision.TemplateMatch.ViewModels
{
    public class DrawShapeViewModel : NavigationViewModel
    {
        public DrawShapeViewModel() 
        {
            this.LoadImageCommand = new DelegateCommand(LoadImage);
            this.DrawObjectList = new ObservableCollection<DrawingObjectInfo>();
        }

        private HObject image;
        public HObject Image
        {
            get { return image; }
            set { 
                image = value;
                RaisePropertyChanged(); 
            }
        }

        private ObservableCollection<DrawingObjectInfo> drawObjectList;

        public ObservableCollection<DrawingObjectInfo> DrawObjectList
        {
            get { return drawObjectList; }
            set { drawObjectList = value; RaisePropertyChanged(); }
        }


        public DelegateCommand LoadImageCommand { get; set; }

        //使用HALCON库和Windows的文件对话框来加载一张图像。
        private void LoadImage()
        {
            //选择一张图像文件。
            OpenFileDialog dialog = new OpenFileDialog();
            //调用dialog.ShowDialog()打开文件选择对话框。这个方法返回一个Nullable<bool>类型的值，表示用户是否成功选择了文件。这个返回值被强制转换为bool类型。
            var dialogResult = (bool)dialog.ShowDialog();
            if (dialogResult)
            {   //HImage是HALCON库中的图像数据类型，用于处理和分析图像。
                var img = new HImage();
                //读取并加载图像
                img.ReadImage(dialog.FileName);
                Image = img;
            }
        }
    }
}
